﻿@inject IOptionsSnapshot<SiteOptions> SiteOptions
@inject IBlogPostService BlogPostService
@inject IDistributedCacheHelper CacheHelper

@{
    const string cacheKeyForCategories = "_ToolsPartial";

    async Task<BlogPostListViewModel> GetDataFromDb()
    {
        BlogPostListViewModel vm = new();
        vm.Categories = new();
        string[] categorySlugs =
        {
            "other-share-software", "other-science-and-technology-life", "other-workplace-statement"
        };
        foreach (var categorySlug in categorySlugs)
        {
            var posts = await BlogPostService.BlogPostBriefListByCategorySlugAsync(new BlogPostBriefListByCategorySlugRequest(categorySlug, 1, 16));
            vm.Categories.Add(new BlogPostListItem()
            {
                CategoryName = posts.CategoryName,
                CategorySlug = categorySlug,
                BlogPosts = posts.Data
            });
        }
        return vm;
    }

    var datas = await CacheHelper.GetOrCreateAsync(cacheKeyForCategories,
        async e => await GetDataFromDb());
}

@if (datas?.Categories is { Count: > 0 })
{
    <div class="wenzi whitebg box tab-num06">
        <div class="gd-box">
            <ul id="tab">
                @{
                    var isSet = false;
                }
                @foreach (var item in datas.Categories)
                {
                    <li class="@(isSet == false ? "tab-current" : "")">@item.CategoryName</li>
                    isSet = true;
                }
            </ul>
        </div>
        <div id="tab-content">
            @foreach (var cat in datas.Categories.Where(cat => cat.BlogPosts is {Count:>0}))
            {
                <section class="tab-box">
                    <div class="down-list">
                        <ul>
                            @foreach (var blogPost in cat.BlogPosts!)
                            {
                                <li>
                                    <a target="_blank" href="@blogPost.CreationTime.ToString("/yyyy/MM")/@blogPost.Slug" class="dl-link">
                                        <i>
                                            <img src="@blogPost.Cover" alt="@blogPost.Description">
                                        </i>
                                        <h2>
                                            @blogPost.Title <span>@(blogPost.CreationTime.ToString("yyyy-MM-dd"))</span>
                                        </h2>
                                        <p>@blogPost.Description</p>
                                    </a>
                                    <a target="_blank" href="@blogPost.CreationTime.ToString("/yyyy/MM")/@blogPost.Slug" class="d-list-hover">
                                        <em>浏览</em>
                                    </a>
                                </li>
                            }
                        </ul>
                    </div>
                    <span class="tab-a-more">
                        <a href="/cat/@cat.CategorySlug" title="更多@(cat.CategoryName)">
                            <i></i><i></i><i></i>
                        </a>
                    </span>
                </section>
            }
        </div>
    </div>
}